<?php

require_once 'classes/LightPressPlugin.php';

class PostsList extends LightPressPlugin {
    
    var $constructor_args = array(
        'date_format'=>'custom date format',
        'limit'=>'nmber of posts to show',
        'skip' =>'number of most recent posts to skip');
    var $default_context = LP_CONTEXT_ALL;
    var $description = 'Display the top n most recent posts';
    
    var $active = true;
    var $hooks = array('sidebar');
    
    var $date_format;
    var $limit = 5;
    var $skip = 0;
    
    function PostsList(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$hook_data) {
        
        $frontend =& $this->_frontend;
        
        $tpl =& $frontend->tpl;
        $db =& $frontend->db;
        $date_format = (empty($this->date_format) ? $frontend->options['date_format'] : $this->_date_format);
        
        $tpl->setFile('plugin_postslist', 'plugins/posts_list.xml');

        $q = "select id, post_title as title, post_name as name, unix_timestamp(post_date) as tstamp, post_category " .
            "from " . $frontend->tables['posts'] . " p " .
            "where (p.post_status='publish') and p.post_date_gmt <= '" . $frontend->where_date . "' " .
            "order by p.post_date desc limit " . ($this->limit + $this->skip);

        if (!$db->query($q))
            return $this->hide();
        
        $posts = array();
        $skipcount = 0;
        foreach ($db->all() as $k => $v) {
            $skipcount++;
            if ($skipcount > $this->skip) {
                $v['permalink'] = $this->_frontend->getPermalink($v['id'], $v['name'], $v['tstamp'], $v['post_category']);
                $v['date'] = strftime($date_format, $v['tstamp']);
                $v['short_date'] = strftime($frontend->options['short_date_format'], $v['tstamp']);
                $posts[] = $v;
            }
        }
        $tpl->parseBlock('PLUGIN_POSTSLIST_POST', 'plugin_postslist_post', $posts, 'plugin_postslist');
        $tpl->parse('PLUGIN_POSTSLIST', 'plugin_postslist');
    }
    
}

?>